home *** CD-ROM | disk | FTP | other *** search
- unit Caseunit;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls;
-
- type
- TCaseTestForm = class(TForm)
- ConvertFromEd: TEdit;
- ConvertToEd: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- CalcBtn: TButton;
- GroupBox1: TGroupBox;
- KmToMilesRBtn: TRadioButton;
- MilesToKmRBtn: TRadioButton;
- CToFRBtn: TRadioButton;
- FToCRBtn: TRadioButton;
- procedure CalcBtnClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- CaseTestForm: TCaseTestForm;
-
- implementation
-
- {$R *.DFM}
-
- function NoError( Code : integer ) : boolean;
- { an example of using a case statement as an alternative to
- multiple if-then-else conditions }
- var
- ErrMsg : string;
- begin
- { tests the error code returned by VAL }
- case Code of
- 1 : ErrMsg := 'First character is not a number';
- 2, 3 : ErrMsg := 'Error found at position 2 or 3';
- 4..10: ErrMsg := 'Error found somewhere between 4 and 10';
- else
- ErrMsg := 'Error at position 11 or above';
- end;
- { if Code = 0 then VAL found no errors and the NoError function returns a
- value of True. Otherwise, if Code is some other value, a message dialog
- displays the ErrMsg assigned by the CASE statement above and the
- NoError function returns False. }
- if Code <> 0 then
- begin
- MessageDlg(ErrMsg, mtWarning, [mbOK], 0 );
- NoError := false;
- end
- else
- NoError := true;
- end;
-
- procedure TCaseTestForm.CalcBtnClick(Sender: TObject);
- const
- milesToKM = 1.60934;
- KMToMiles = 0.62137;
- var
- InputVal, OutputVal : Real;
- Code : integer;
- S : string;
- begin
- Val(ConvertFromEd.Text, InputVal, Code );
- { Calls the NoError function to test the Code variable returned by VAL.
- If there is no error, the calculation between the following begin-end pair
- is performed. If there is an error, the lines between the begin-end pair
- are skipped }
- If NoError( Code ) Then
- begin { - start of the bracketing begin-end pair - }
- If KmToMilesRBtn.checked then
- OutputVal := InputVal * KmToMiles
- else if MilesToKmRBtn.checked then
- OutputVal := InputVal * milesToKM
- else if CToFRBtn.checked then
- OutputVal := ((InputVal * 9) / 5) + 32
- else if FToCRBtn.checked then
- OutputVal := ((InputVal -32) * 5) /9;
- Str( OutputVal:2:2, S );
- ConvertToEd.Text := S;
- end; { - end of the bracketing begin-end pair - }
- end;
-
- end.
-